// pages/mine/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationDisplay:true,
    last_scroll_position:0,
    open:'',
    isMember:false,
    is_leader:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var open=wx.getStorageSync('open');
    if(open==undefined){

      wx.navigateTo({
        url: '../index/login',
      })
     }
     if(open==""){
  
      wx.navigateTo({
        url: '../index/index',
      })
     } 
    console.log('open');
    var member_id=open.member_id;
   console.log(member_id);
   //member_id=0;
    if(member_id){
      this.setData({
        isMember:true,
        member_id:member_id
      })
     var open_id=open.id;
      this.getMy(member_id);
      this.noticeUpdate(open_id);
    } 
    this.setData({
      open:open
    })
     console.log(this.data.isMember)
     this.setData({
      width:app.globalData.windowWidth,
      height:app.globalData.windowHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      navHeight:app.globalData.navHeight,
      marginTop:app.globalData.navTitleTop
    })
  },

  noticeUpdate:function(open_id){
    let that=this;
       wx.request({
         url: app.globalData.domain+'member/notice/mineUpdate',
         data:{open_id:open_id},
         success:res=>{
             console.log(res);
             var noticeNumber=res.data.noticeNumber;
             var notice=that.data.notice;
             if(noticeNumber>0){
               notice=true;
             }else{
               notice=false;
             }
             that.setData({
               notice:notice,
               noticeNumber:noticeNumber
             })
         }
       }) 
  },
  gotoLogin(){ 
    app.globalData.prevpage='/mine/index';
    wx.navigateTo({
      url: '../index/getUserProfile',
    })
  },
  getPhoneNumber:function (e) {
    var open=wx.getStorageSync('open');
    console.log(e);
 
            wx.request({
                url:app.globalData.domain+'wechat/login/getPhone',
                data:{
                   "id":open.id,
                   "openid":open.mini_openid,
                   'code':e.detail.code
                },
                method:'GET',
                header:{
                    'content-type':'application/json'
                },
                success:function(res){
                  console.log(res)
                    //wx.setStorageSync('PhoneNumber',res.data.phone);
                    if(res.data.code==200){
                      open.phone = res.data.phone;
                      wx.setStorageSync('open', open);
                      this.setData({
                        open:open
                      })
                      wx.showToast({
                        title: '手机号成功',
                        success:function(res){
                          //wx.navigateBack();
                        }
                      })
                    }else{
                      wx.showToast({
                        title: '手机号获取失败'
                      })
                    } 
                    //console.log("手机号为");
                    //console.log(res);
                },
                fail:function(err){
                    console.log(err);
                }
            })
      
   
  },
  getMy:function(member_id){
    let that=this;
    //var member_id=this.data.member_id;
    wx.request({
      url:app.globalData.domain+'/member/member/getMemberById',
      data:{id:member_id},
      success:res=>{
        var member=res.data.member;
        console.log(member);
        if(res.data.status==1){
          that.setData({
            member:member,
            is_leader:member.is_leader
          })
        }
      
      }
    })
    
  },
  getUserProfile: function(e) {
    var that=this;    
    wx.getUserProfile({
      desc:'头像 昵称 性别',
      success: res => {
        console.log(res)
        var open=wx.getStorageSync('open');
        var unionid=open.unionid;
        var id=open.id;
        wx.request({
          url: app.globalData.domain+'member/login/setOpen',
    
          data:{id:id,unionid:unionid,nickname:res.open.nickName,avatarUrl:res.open.avatarUrl},
    
          success: (s) => {
            console.log(res.data);
            //console.log(res.data.unionid);
            //pp.globalData.open = res.data 
            //wx.setStorageSync('open', res.data)
            if(s.data==1){
              open['nickname']=res.open.nickName;
              open['avatarUrl']=res.open.avatarUrl;
              open['status']=1;
              wx.setStorageSync('open',open);
              wx.reLaunch({
              url: '/pages/default/index',
            })
            }
           
           
          }    
    
        })
        //console.log("tt")
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addMember:function(){
    if(this.data.isMember==0){
      wx.navigateTo({
        url: '../member/add',
      }) 
    }else{
      wx.showModal({
        title: '重要提示',
        content:'您已经是会员'
      })
    }
    
  },
  member:function(){
    if(this.data.isMember==1){
      wx.navigateTo({
        url: 'member',
      }) 
    }else{
      wx.showModal({
        title: '重要提示',
        content:'您还不是会员'
      })
    }
  },
  paylist:function(){
    if(this.data.isMember==1){
      wx.navigateTo({
        url: 'payList',
      }) 
    }else{
      wx.showModal({
        title: '重要提示',
        content:'您还不是会员'
      })
    }
  },
  onPageScroll: function (e){

    var that = this

    var last_scroll_position = e.scrollTop;
    var navigationDisplay;
    if(last_scroll_position>0){
      navigationDisplay=false;
    }else{
      navigationDisplay=true;
    }
    this.setData({
      last_scroll_position:last_scroll_position,
      navigationDisplay:navigationDisplay
    })
  },
  goto:function(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  }
})